﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireStore.Models;
using System.Data;
using TireStore.General;
using PagedList;
using PagedList.Mvc;

namespace TireStore.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        readonly TireStoreContext _data = new TireStoreContext();

        public ActionResult Index(int? page, int? curpage, int? sizpage, int? funcName, int? productId)
        {
            var pageSize = (sizpage ?? 5);
            var pageNum = (page ?? 1);

            ViewBag.curPage = page;

            switch (funcName)
            {
                case 1:
                    var product = _data.Products.Find(productId);
                    if (product != null)
                    {
                        _data.Products.Remove(product);
                        _data.SaveChanges();
                    }
                    break;
            }

            var x = new PagedListRenderOptions
            {
                MaximumPageNumbersToDisplay = 3,
                LinkToFirstPageFormat = "Đầu",
                LinkToLastPageFormat = "Cuối",
                LinkToPreviousPageFormat = "«",
                LinkToNextPageFormat = "»",
                DisplayLinkToFirstPage = PagedListDisplayMode.Always,
                DisplayLinkToLastPage = PagedListDisplayMode.Always,
                DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
                DisplayLinkToNextPage = PagedListDisplayMode.Always
            };

            ViewBag.CustomPage = x;

            using (var db = new TireStoreContext())
            {
                var products = db.Products.Include("Brand").Include("ProductGroup").ToList().ToPagedList(pageNum, pageSize);
                return View( products);
            }
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var product = _data.Products.Find(id);

            if (product != null)
            {
                ViewBag.BrandId = new SelectList(_data.Brands, "Id", "Name", product.BrandId);
                ViewBag.ProductGroupId = new SelectList(_data.ProductGroups, "Id", "Name", product.ProductGroupId);

                return View(product);

            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(FormCollection form, Product product)
        {

            if (ModelState.IsValid)
            {
                product.Img = "x";
                var pro = product;
                _data.Entry(pro).State = EntityState.Modified;
                _data.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.BrandId = new SelectList(_data.Brands, "Id", "Name", product.BrandId);
                ViewBag.ProductGroupId = new SelectList(_data.ProductGroups, "Id", "Name", product.ProductGroupId);
                return View();
            }

        }

        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.BrandId = new SelectList(_data.Brands, "Id", "Name");
            ViewBag.ProductGroupId = new SelectList(_data.ProductGroups, "Id", "Name");
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(ProductInfo product)
        {
            product.Img = "x";
            if (ModelState.IsValid)
            {
                _data.Products.Add(new Product
                {
                    Name = product.Name,
                    Code = product.Code,
                    Img = product.Img,
                    ProductGroupId = product.ProductGroupId,
                    BrandId = product.BrandId,
                    MIindex = product.MIindex,
                    Active = product.Active,
                    Price = product.Price
                });
                _data.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.BrandId = new SelectList(_data.Brands, "Id", "Name");
                ViewBag.ProductGroupId = new SelectList(_data.ProductGroups, "Id", "Name");
                return View(product);
            }
        }

        

        //[HttpPost]
        //public ActionResult Delete(int productId)
        //{
        //    var x = productId;
        //    return Json()
        //}

    }
}
